package org.jeecgframework.web.tcdev.service.impl;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.dao.JeecgDictDao;
import org.jeecgframework.web.tcdev.entity.TArticle;
import org.jeecgframework.web.tcdev.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 文章管理业务层实现类
 * Created by zjy on 2015/4/13.
 */
@Service("articleService")
@Transactional
public class ArticleServiceImpl extends CommonServiceImpl implements ArticleService {

    @Autowired
    private JeecgDictDao jeecgDictDao;

    @Override
    public void addArticleAndHasCategory(TArticle article, String[] articleCategoryIds) throws Exception {

        //判断是否发布，如果是发布，那么存入当前时间做为发布时间
        if("1".equals(article.getIsPublish())){

            article.setPublishTime(new Date());

        }

        if(article.gettAttachment() != null && StringUtil.isNotEmpty(article.gettAttachment().getAttachmentId())){

            //保存附件
            this.save(article.gettAttachment());
        }else{

            article.settAttachment(null);
        }


        //保存文章
        this.save(article);


        //关系插入SQL
        String hasSQL = "insert into t_article_has_category(article_id,article_category_id) values(?,?)";

        //维护文章与分类的关系
        for(String cateId : articleCategoryIds){

            if(!"".equals(cateId)){

                this.executeSql(hasSQL,new Object[]{article.getId(),cateId});

            }

        }

    }

    @Override
    public void updateArticleAndHasCategory(TArticle article, String[] articleCategoryIds) throws Exception {


        TArticle t = this.getEntity(TArticle.class,article.getId());


        if(t.getPublishTime() == null){
            //判断是否发布，如果是发布，那么存入当前时间做为发布时间
            if("1".equals(article.getIsPublish())){

                article.setPublishTime(new Date());

            }
        }

        MyBeanUtils.copyBeanNotNull2Bean(article, t);

        if(t.gettAttachment() != null && StringUtil.isEmpty(t.gettAttachment().getAttachmentId())){

            this.save(t.gettAttachment());

        }
        this.getSession().merge(t);

        //修改文章
        this.saveOrUpdate(t);

        //删除关联关系
        this.executeSql("delete from t_article_has_category where article_id=?",new String[]{article.getId()});

        //关系插入SQL
        String hasSQL = "insert into t_article_has_category(article_id,article_category_id) values(?,?)";


        //维护文章与分类的关系
        for(String cateId : articleCategoryIds){

            if(!"".equals(cateId)){

                this.executeSql(hasSQL, new Object[]{article.getId(), cateId});

            }

        }
    }

    @Override
    public void deleteArticleAndHasCategory(TArticle sArticle) throws Exception {



        //删除文章
        this.delete(sArticle);

        //删除附件数据
        //this.delete(sArticle.getsAttachment());

        //删除关联关系
        this.executeSql("delete from t_article_has_category where article_id=?",new String[]{sArticle.getId()});

    }

}
